package com.jackey.sms.exception;

import com.jackey.sms.utils.Result;
import com.jackey.sms.utils.ResultCode;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 小伟
 * @version V1.0
 * @Package com.jackey.sms.exception
 * @date 2020/12/20 16:42
 * @Copyright © 2020-2021 郑州恒星科技有限责任公司
 */
@ControllerAdvice
public class BaseExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(HttpServletRequest request, HttpServletResponse response, Exception e) {
        System.out.println(e.getClass());
        e.printStackTrace();
        if (UnauthorizedException.class==e.getClass()){
            return new Result(ResultCode.UNAUTHORISE);
        }
       else if (e.getClass() == AuthorizationException.class) {
            return new Result(ResultCode.UNAUTHORISE);
        }else if (e.getClass() == UnauthenticatedException.class){
            return new Result(ResultCode.FAIL);
        }
        else{
            return  new Result(ResultCode.SERVER_ERROR);
        }
    }
}
